function WindView() {
	this.views = new Array();
	this.setCssClass("WindView");
	this.value = "";
	this.resizeable = false;
}

WindView.prototype.addView = function(childView) {
	this.views[this.views.length] = childView;
	childView.setParentView(this);
	
	if(this.getHtmlElement()!=null) {
		if(childView.getHtmlElement()==null) {
			var htmlElement = childView.render();
			this.getHtmlElement().appendChild(htmlElement);
		} else {
			this.getHtmlElement().appendChild(childView.getHtmlElement());
		}
		this.applyFullscreenToHtmlElement();
		this.applyCenteredToHtmlElement();
	}
}

WindView.prototype.removeView = function(view) {
	var htmlElement = view.getHtmlElement();
	if(htmlElement.parentNode!=null) {
		htmlElement.parentNode.removeChild(htmlElement);
	}
}

WindView.prototype.getViews = function () {
	return this.views;
}

WindView.prototype.getNumberOfViews = function () {
	return this.views.length;
}


WindView.prototype.getView = function (index) {
	return this.views[index];
}


WindView.prototype.clearViews = function() {
	this.views.length = 0;
	if(this.getHtmlElement()!=null) {
		while (this.getHtmlElement().hasChildNodes()) {
			this.getHtmlElement().removeChild(this.getHtmlElement().lastChild);
		}
	}
}

WindView.prototype.createHtmlElement = function() {
	var htmlElement = document.createElement("div");
	return htmlElement;
}

WindView.prototype.render = function() {
	var htmlElement = null;

	htmlElement = this.createHtmlElement();
	
	var view = this;
	htmlElement.getView = function() {
		return view;
	}
	
	this.setHtmlElement(htmlElement);
		
	this.applyViewAttributesToHtmlElement(htmlElement);

	this.renderChildren(htmlElement);
		
	return htmlElement;
}

WindView.prototype.renderChildren = function(parent) {
	var childViews = this.getViews();
	for(var i=0; i<childViews.length; i++) {
		var childView = childViews[i];
		var childDiv = childView.render();
		if(childDiv!=null) {
			parent.appendChild(childDiv);
		}
	}
}


WindView.prototype.applyFullscreenToHtmlElement = function() {
	var div = this.getHtmlElement();
	if(div!=null && this.isFullscreen()) {
		div.style.top = "0px";
		div.style.left = "0px";
		div.style.width = WindWindow.getWindowWidth() + "px";
		div.style.height = WindWindow.getWindowHeight() + "px";
	}
}

WindView.prototype.applyCenteredToHtmlElement = function() {
	var div = this.getHtmlElement();
	if(div!=null && this.isCentered()) {
		var top = document.offsetHeight - div.offsetHeight/2;
		div.style.top = top + "px"
		var left = document.offsetWidth - div.offsetWidth/2;
		div.style.left = left + "px"
	}
}

WindView.prototype.applyViewAttributesToHtmlElement = function(div) {

		var id = this.getId();
		if(id!=null) {
			div.id = id;
		}

		var cssClass = this.getCssClass();
		if(cssClass!=null) {
			div.className = cssClass;
		}

		var x = this.getX();
		if(x!=null) {
			div.style.left = x;
		}
		
		var y = this.getY();
		if(y!=null) {
			div.style.top = y;
		}

		var width = this.getWidth();
		if(width!=null) {
			div.style.width = width;
		}

		var height = this.getHeight();
		if(height!=null) {
			div.style.height = height;
		}

		var backgroundColor = this.getBackgroundColor();
		if(backgroundColor!=null) {
			div.style.backgroundColor = backgroundColor;
		}

		if(!this.visible()) {
			div.style.display = "none";
		} else {
			div.style.display = "";
		}

		var text = this.getText();
		if(text!=null) {
			div.innerHTML = text;
		}
		
		var image = this.getImage();
		if(image!=null) {
			var img = document.createElement("img");
			img.src = image;
			div.appendChild(img);
		}

		var onClick = this.onClick;
		if(onClick!=null) {
			div.addEventListener('click', onClick, false);
		}
		
		var onContextMenu = this.onContextMenu;
		if(onContextMenu!=null) {
			div.oncontextmenu = onContextMenu;
			//div.addEventListener('contextmenu', onContextMenu, true);
			//div.addEventListener('mousedown', function(e){e.stopPropagation();return false;}, true);
			//div.addEventListener('mouseup', function(e){e.stopPropagation();return false;}, true);
		}
		
		var onDblClick = this.onDblClick;
		if(onDblClick!=null) {
			div.addEventListener('dblclick', onDblClick, false);
		}

		var onMouseOut = this.onMouseOut;
		if(onMouseOut!=null) {
			div.addEventListener('mouseout', onMouseOut, false);
		}

		var onMouseDown = this.onMouseDown;
		if(onMouseDown!=null) {
			div.addEventListener('mousedown', onMouseDown, false);
		}

		var onMouseUp = this.onMouseUp;
		if(onMouseUp!=null) {
			div.addEventListener('mouseup', onMouseUp, false);
		}

		var onMouseOver = this.onMouseOver;
		if(onMouseOver!=null) {
			div.addEventListener('mouseover', onMouseOver, false);
		}

		var onMouseMove = this.onMouseMove;
		if(onMouseMove!=null) {
			div.onmousemove = onMouseMove;
			div.addEventListener('mouseMove', onMouseMove, false);
		}

		var onBlur = this.onBlur;
		if(onBlur!=null) {
			div.addEventListener('blur', onBlur, false);
		}

		var onKeyPress = this.onKeyPress;
		if(onKeyPress!=null) {
			div.addEventListener('keypress', onKeyPress, false);
		}
		
		var view = this;
		div.addEventListener('mouseover', function(e) {WindView.defaultOnMouseOver(e, view);}, false);
}

WindView.prototype.setId = function (id) {
	this.id = id;
}

WindView.prototype.getId = function () {
	return this.id;
}

WindView.prototype.setModel = function (model) {
	this.model = model;
}

WindView.prototype.getModel = function () {
	return this.model;
}

WindView.prototype.setFullscreen = function (fullscreen) {
	this.fullscreen = fullscreen;
}

WindView.prototype.isFullscreen = function () {
	if(this.fullscreen == null) {
		this.fullscreen = false;
	}
	return this.fullscreen;
}

WindView.prototype.setCentered = function (centered) {
	this.centered = centered;
}

WindView.prototype.isCentered = function () {
	if(this.centered == null) {
		this.centered = false;
	}
	return this.centered;
}

WindView.prototype.setHtmlElement = function (div) {
	this.div = div;
}

WindView.prototype.getHtmlElement = function () {
	return this.div;
}

WindView.prototype.setController = function (controller) {
	this.controller = controller;
}

WindView.prototype.getId = function () {
	return this.controller;
}


WindView.prototype.setCssClass = function (cssClass) {
	this.cssClass = cssClass;
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().className = cssClass;
	}
}

WindView.prototype.getCssClass = function () {
	return this.cssClass;
}

WindView.prototype.setParentView = function (parentView) {
	this.parentView = parentView;
}

WindView.prototype.getParentView = function () {
	return this.parentView;
}

WindView.prototype.setId = function (id) {
	this.id = id;
}

WindView.prototype.getId = function () {
	return this.id;
}

WindView.prototype.setText = function (text) {
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().innerHTML = text;
	}
	this.text = text;
}

WindView.prototype.getText = function () {
	return this.text;
}

WindView.prototype.setImage = function (image) {
	this.image = image;
}

WindView.prototype.getImage = function () {
	return this.image;
}

WindView.prototype.setX = function (x) {
	this.x = x;
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().style.left = x + "px";
	}
}

WindView.prototype.getX = function () {
	return this.x;
}

WindView.prototype.setY = function (y) {
	this.y = y;
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().style.top = y + "px";
	}
}

WindView.prototype.getY = function () {
	return this.y;
}

WindView.prototype.setWidth = function (width) {
	this.width = width;
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().style.width = width + "px";
	}
}

WindView.prototype.getWidth = function () {
	return this.width;
}

WindView.prototype.setHeight = function (height) {
	this.height = height;
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().style.height = height + "px";
	}
}

WindView.prototype.getHeight = function () {
	var height = this.height;
	if(this.getHtmlElement()!=null) {
		//height = this.getHtmlElement().offsetHeight;
	}
	return height;
}

WindView.prototype.setBackgroundColor = function (backgroundColor) {
	this.backgroundColor = backgroundColor;
}

WindView.prototype.getBackgroundColor = function () {
	return this.backgroundColor;
}


WindView.prototype.hide = function () {
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().style.display = "none";
	}

	this.hidden = true;
}

WindView.prototype.show = function () {
	if(this.getHtmlElement()!=null) {
		this.getHtmlElement().style.display = "";
	}

	this.hidden = false;
}

WindView.prototype.visible = function () {
	return !this.hidden;
}

WindView.prototype.setValue = function (value) {
	this.value = value;
}

WindView.prototype.getValue = function () {
	return this.value;
}

WindView.prototype.setContextMenu = function (view) {
	this.view = view;
}

WindView.prototype.getContextMenu = function () {
	return this.view;
}

WindView.prototype.setPath = function (path) {
	this.path = path;
}

WindView.prototype.getPath = function () {
	return this.path;
}

var __wind__lastId = 0;

WindView.generateId = function() {
	var newId = __wind__lastId + 1;
	__wind__lastId = newId;
	return newId;	
}

WindView.prototype.setResizeable = function (resizeable) {
	this.resizeable = resizeable;
}

WindView.prototype.getResizeable = function () {
	return this.resizeable;
}

WindView.defaultOnMouseOver = function (event, view) {
	if(view.getResizeable!=null && view.getResizeable()) {
		var elem = view.getHtmlElement();
		var elemHeight = Element.getHeight(elem);
		var elemWidth = Element.getWidth(elem);
		var elemPosition = Element.cumulativeOffset(elem);
		var x = Event.pointerX(event) - elemPosition.left;
		var y = Event.pointerY(event) - elemPosition.top;
		//view.setText(x + "," + y);
		var border = 5;
		if(y < border) {
			elem.style.cursor = "ns-resize";
			elem.addEventListener('mousedown', function(e){WindView.startResizeing(e,view,"top");}, false);
		} else if(y > (elemHeight-border)) {
			elem.style.cursor = "ns-resize";
			elem.addEventListener('mousedown', function(e){WindView.startResizeing(e,view,"bottom");}, false);
		} else {
			if(x < border) {
				elem.style.cursor = "ew-resize";
				elem.addEventListener('mousedown', function(e){WindView.startResizeing(e,view,"left");}, false);
			} else if (x > (elemWidth-border)) {
				elem.style.cursor = "ew-resize";
				elem.addEventListener('mousedown', function(e){WindView.startResizeing(e,view,"right");}, false);
			} 
		}
	} 
}

WindView.resizeingView = null;

WindView.startResizeing = function(event,view,type) {
	WindView.resizeingView = view;	
	var elem = view.getHtmlElement();
	document.onmouseup = function(e) {
		WindView.stopResizeing();
		Event.stop(e)
	}
	document.onmousemove = function(e) {
		var border = 3;
		if(WindView.resizeingView!=null) {
			if(type=="bottom") {
				elem.style.cursor = "ns-resize";
				var elemCumulativeOffset = Element.cumulativeOffset(elem);
				var newY = Event.pointerY(e) + border;
				elem.style.height = (newY - elemCumulativeOffset.top) + "px";
			} else if(type=="top") {
				elem.style.cursor = "ns-resize";
				var elemOffsetTop = elem.offsetTop; 
				var elemCumulativeOffset = Element.cumulativeOffset(elem);
				var newY = Event.pointerY(e);
				var oldHeight = Element.getHeight(elem);
				elem.style.top = (newY - elemCumulativeOffset.top + elemOffsetTop - border) + "px"; 
				elem.style.height = (oldHeight (newY - elemCumulativeOffset.top)) + "px";
			} else if(type=="right") {
				elem.style.cursor = "ew-resize";
				var elemCumulativeOffset = Element.cumulativeOffset(elem);
				var newX = Event.pointerX(e);
				elem.style.width = newX - elemCumulativeOffset.left + border;
			} else if(type=="left") {
				elem.style.cursor = "ew-resize";
				var elemOffsetLeft = elem.offsetLeft; 
				var elemCumulativeOffset = Element.cumulativeOffset(elem);
				var newX = Event.pointerX(e);
				var oldWidth = Element.getWidth(elem);
				elem.style.left = (newX - elemCumulativeOffset.left + elemOffsetLeft - border) + "px"; 
				elem.style.width = oldWidth - (newX + elemOffsetLeft - elem.offsetLeft) + "px";
				WindLogger.log(elem.offsetLeft + " - " + elemOffsetLeft);
			}
			Event.stop(e)
		}
	}
	Event.stop(event)
}

WindView.stopResizeing = function() {
	WindView.resizeingView = null;
}
